package com.xtoad.ecms.common.utils.generate;

import java.util.Map;

/**
 * 数据持久层代码生成器
 *
 * @author xtoad
 * @date 2021/1/19
 */
public class RepositoryGenerator implements IGenerator {
    /**
     * 根据包路径和类名生成类
     *
     * @param packagePath 包路径
     * @param modelName   类名
     * @return 生成的类
     */
    @Override
    public String generate(String packagePath, String modelName) {
        System.out.println("generate Repository start ");
        final String repositoryName = "I{0}Repository";
        Map<String, String> classComment = Generator.getClassComment(Generator.modelPackage + "." + modelName);
        StringBuilder sb = new StringBuilder();
        sb.append("package ").append(packagePath).append(";\r\n\r\n")
                .append("import org.springframework.data.domain.Page;\r\n")
                .append("import org.springframework.data.domain.Pageable;\r\n")
                .append("import org.springframework.data.jpa.repository.JpaRepository;\r\n")
                .append("import org.springframework.data.repository.PagingAndSortingRepository;\r\n")
                .append("import org.springframework.stereotype.Repository;\r\n")
                .append("\r\n")
                .append("import ").append(Generator.modelPackage).append(".").append(modelName).append(";\r\n\r\n")
                .append("import java.util.List;\r\n\r\n")
                .append("/**\r\n")
                .append(" * ").append(classComment.get(modelName)).append("Repository类\r\n")
                .append(" *\r\n")
                .append(" * @author ").append(Generator.author).append("\r\n")
                .append(" * @date ").append(Generator.dateString).append("\r\n")
                .append(" */\r\n")
                .append("@Repository\r\n")
                .append("public interface I").append(modelName).append("Repository extends JpaRepository<")
                .append(modelName).append(", Long>, PagingAndSortingRepository<").append(modelName).append(", Long> {\r\n")
                .append("}\r\n");

        System.out.println("generate Repository end ");

        return sb.toString();
    }
}
